//
//  WSMoreViewController.m
//  GroupFourFM
//
//  Created by dllo on 15/12/28.
//  Copyright © 2015年 GroupFour. All rights reserved.
//

#import "WSMoreViewController.h"
#import "WSPlayViewController.h"
#import "WSResource.h"
#import "WSChildListModel.h"
#import "WHCategoryTableViewCell.h"
#import "WSQualityCell.h"
#import "WSAlbumsDetailViewController.h"
#import "WSRecommendDetailController.h"
#import <BmobSDK/Bmob.h>


@interface WSMoreViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) WSBaseTableView *myTableView;
@property (nonatomic, strong) NSMutableArray *myArray;
@property (nonatomic, assign) NSInteger currentPage;

@end

@implementation WSMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //初始化数组
    self.myArray = [NSMutableArray array];
    
    [self createTableView];
    
    //返回按钮
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0 , 20, 30);
    backButton.backgroundColor = CLEAR;
    [backButton setImage:[UIImage imageNamed:@"btn_back_n@2x.png"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"btn_back_h@2x.png"] forState:UIControlStateHighlighted];
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.title = self.myTitle;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.currentPage = 1;
}

#pragma mark - playButton
-(void)buttonAction:(UIButton *)btn
{
    
    WSPlayViewController *player = [WSPlayViewController sharaMusicPlayer];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:player];
//        WSTrackModel *sing = [self.allTrackArray objectAtIndex:indexPath.row];
//        player.song_id = sing.trackId;
//        player.allMusicArray = self.allTrackArray;
//        player.indexRow = indexPath.row;
    [self presentViewController:naVC animated:YES completion:^{
        
    }];
    
}

#pragma mark - 返回按钮方法
-(void)backButtonAction:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
    //    [[NSNotificationCenter defaultCenter] postNotificationName:@"createBigButton" object:[TOPlayButton shareButton]];
    
}

#pragma mark - 创建tableView
-(void)createTableView
{
    self.myTableView = [[WSBaseTableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    [self.myTableView registerClass:[WSQualityCell class] forCellReuseIdentifier:@"moreListTableCell"];
    [self.myTableView registerClass:[WHCategoryTableViewCell class] forCellReuseIdentifier:@"editorListTableCell"];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(@0);
    }];
    
    //头部刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(upTableView)];
    self.myTableView.mj_header = header;
    
    [self.myTableView.mj_header beginRefreshing];
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(downTableView)];
    self.myTableView.mj_footer = footer;
    
    
    
}

#pragma mark - 下拉刷新
- (void)upTableView{
    
    
    if ([self.myTitle isEqualToString:@"精品听单"]) {
        self.moreUrl = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/subject_list?device=iphone&page=%ld&per_page=10&title=更多",self.currentPage];
    }
    else
    {
        self.moreUrl = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/recommend/editor?device=iphone&pageId=%ld&pageSize=20&title=更多",self.currentPage];
    }
    
    [self addData: self.moreUrl andDwon:NO];
}

#pragma mark - 上拉刷新
- (void)downTableView{
    
    if ([self.myTitle isEqualToString:@"精品听单"]) {
        self.moreUrl = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/subject_list?device=iphone&page=%ld&per_page=10&title=更多",self.currentPage+1];
    }
    else
    {
        self.moreUrl = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/recommend/editor?device=iphone&pageId=%ld&pageSize=20&title=更多",self.currentPage+1];
    }
    
    [self addData:self.moreUrl andDwon:YES];
}

- (void)addData:(NSString *)url andDwon:(BOOL)isDown
{
    
#pragma mark - 查询收藏列表,给Model赋值
    
    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"Collect_album"];
    //查找GameScore表的数据
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {

        
        [WSResource getDataWithURL:url block:^(id result) {
        
            NSMutableArray *dataArray = [result objectForKey:@"list"];
        
            for (NSMutableDictionary *dic in dataArray) {
                if ([self.myTitle isEqualToString:@"精品听单"]) {
                    WSChildListModel *model = [WSChildListModel modelWithDictionary:dic];
                    [self.myArray addObject:model];
                }
                else if ([self.myTitle isEqualToString:@"小编推荐"])
                {
                    WSAlbumModel *model = [WSAlbumModel modelWithDictionary:dic];
                    model.isFavor = NO;
                
                    for (BmobObject *objc in array) {
                        
                        if ([[objc objectForKey:@"albumName"] isEqualToString:model.title]) {
                            
                            model.objectId = [objc objectId];
                            model.isFavor = YES;
                            
                        }
                    }

                    [self.myArray addObject:model];
                }
            }
        
            if (isDown) {
                self.currentPage++;
            }
        
            [self.myTableView.mj_header endRefreshing];
            [self.myTableView.mj_footer endRefreshing];
            [self.myTableView reloadData];
        } error:^(NSError *error) {
        
            NSLog(@"请求失败  error");
        }];
    
    }];
}



#pragma mark - tableView协议方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.myArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.myTitle isEqualToString:@"精品听单"]) {
        WSQualityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"moreListTableCell"];
        
        WSChildListModel *model = [self.myArray objectAtIndex:indexPath.row];
        model.coverPath = model.coverPathSmall;
        cell.textModel = model;
        return cell;
    }
    else
    {
        WHCategoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"editorListTableCell"];
        WSAlbumModel *model = [self.myArray objectAtIndex:indexPath.row];
        cell.albumModel = model;
        return cell;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_WIDTH * 0.3;
}

#pragma mark 点击cell跳转
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([self.myTitle isEqualToString:@"精品听单"]){
        WSRecommendDetailController *listVC = [[WSRecommendDetailController alloc] init];
        listVC.specialId = ((WSChildListModel *)self.myArray[indexPath.row]).specialId;
        [self.navigationController pushViewController:listVC animated:YES];
    }
    else if ([self.myTitle isEqualToString:@"小编推荐"])
    {
        
        WSAlbumsDetailViewController *albumsDetailVC = [[WSAlbumsDetailViewController alloc] init];
        albumsDetailVC.albumId = ((WSAlbumModel *)self.myArray[indexPath.row]).myId;
        albumsDetailVC.myTitle = _myTitle;
        [self.navigationController pushViewController:albumsDetailVC animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
